package com.analysis

//客户数最多的十个国家
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

object TopCountriesByCustomers {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("Top 10 Countries by Customer Count")
      .master("local[*]")
      .getOrCreate()

    val cleanedDF = spark.read.parquet("output/cleaned_data.parquet")

    val resultDF = cleanedDF
      .select("Country", "CustomerID")
      .distinct()
      .groupBy("Country")
      .count()
      .withColumnRenamed("count", "CustomerCount")
      .orderBy(desc("CustomerCount"))
      .limit(10)

    resultDF.write
      .mode("overwrite")
      .json("output/analysis_results/top_countries_by_customers")

    resultDF.show()

    spark.stop()
  }
}